package com.huawei.hianalytics.log.a;

import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

public class b {
    private SortedMap<String, String> a = new TreeMap();

    public b(String str) {
        b(str);
    }

    private void b(String str) {
        if (!TextUtils.isEmpty(str)) {
            for (String str2 : str.split(ContainerUtils.FIELD_DELIMITER)) {
                int indexOf = str2.indexOf(ContainerUtils.KEY_VALUE_DELIMITER);
                this.a.put(str2.substring(0, indexOf), str2.substring(indexOf + 1));
            }
        }
    }

    public String a() {
        StringBuilder stringBuilder = new StringBuilder(WXMediaMessage.TITLE_LENGTH_LIMIT);
        try {
            for (Entry entry : this.a.entrySet()) {
                String str = (String) entry.getKey();
                if (!TextUtils.isEmpty(str)) {
                    String encode = URLEncoder.encode(str, "UTF-8");
                    if (stringBuilder.length() > 0) {
                        stringBuilder.append(ContainerUtils.FIELD_DELIMITER);
                    }
                    String str2 = (String) entry.getValue();
                    stringBuilder.append(encode).append(ContainerUtils.KEY_VALUE_DELIMITER).append(!TextUtils.isEmpty(str2) ? URLDecoder.decode(str2, "UTF-8") : "");
                }
            }
            return stringBuilder.toString();
        } catch (UnsupportedEncodingException e) {
            com.huawei.hianalytics.g.b.c("CanonicalQueryString", "Exception when toString,Encode does not support!");
            return "";
        }
    }

    public String a(String str) {
        String str2 = (String) this.a.get(URLEncoder.encode(str, "UTF-8"));
        return !TextUtils.isEmpty(str2) ? URLDecoder.decode(str2, "UTF-8") : str2;
    }
}
